package Bads07Hash;

import stdlib.*;
import java.util.ArrayList;

public class Gorilla {
	private static int kGram = 20;
	private static int spread = 10000;
	private static String[] names = new String[12];
	private static ArrayList<int[]> list = new ArrayList<int[]>();
	private static double[][] spreadSheet = new double[12][12];
	
	
	public Gorilla() {
		readInfo();
		calcAnglesBetweenRaces();
		printSheet();
	}
	
	public static void readInfo() {
		int k = 0;
		while (!StdIn.isEmpty()) {
			int[] arr = new int[spread];
			names[k] = (String)StdIn.readLine();
			String tempDna = StdIn.readLine();
//			System.out.println(names[k] + " " + tempDna);
			for(int i = 0; i < tempDna.length()-kGram; i++){
				int value = (tempDna.substring(i, i+kGram).hashCode() % spread);
				if (value < 0) {
					value = value * -1;
				}
//				System.out.println(value);
				arr[value]++;
			}	
			list.add(arr);
			k++;
		}
	}
	
	public static double calculateAngle(int[] arr1, int[] arr2){
		double val = 0d, arr1Length = 0, arr2Length = 0;
		for(int i = 0; i < arr1.length; i++){
			double a = arr1[i];
			double b = arr2[i];
			val += (a * b);
			arr1Length += (arr1[i] * arr1[i]);
			arr2Length += (arr2[i] * arr2[i]);
		}
		arr1Length = Math.sqrt(arr1Length);
		arr2Length = Math.sqrt(arr2Length);
		double value = ((val /(arr1Length * arr2Length)));
		
		return value;		
	}
	
	public static void calcAnglesBetweenRaces(){
		for(int i = 0; i < list.size(); i++){
			for(int j = 0; j < list.size(); j++){
				double angle = calculateAngle(list.get(i), list.get(j));
				spreadSheet[i][j] = angle;
			}
		}		
	}
	
	public static void printSheet(){
		String format = "%1$-13s";
		System.out.format(format, "");
		for(int i = 0; i < names.length; i++){
			System.out.format(format, names[i]);
		}
		System.out.println("");
		for(int i = 0; i < names.length; i++){
			format = "%1$-13s";
			System.out.format(format, names[i]);
			format = "%10.2f |";
			for(int k = 0; k < names.length; k++){
				System.out.format(format, spreadSheet[i][k]);
			}
			System.out.println("");
		}
	}
		
	public static void main(String[] args) {
		readInfo();
		calcAnglesBetweenRaces();
//		System.out.println(spreadSheet[0][9]);
		printSheet();
	}
}